<template>
  <div>
    <div class="table-data" v-show="tabShow"
    >
      <div class="search-box">
        <el-input size="small" v-model="searchMap.keyword " placeholder="商家名称"></el-input>
        <el-select v-model="searchMap.cate_id" clearable placeholder="请选择商家类型">
          <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
        <el-button size="small" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        <el-button size="small" type="primary" @click="alltableData">全部商家</el-button>
        <el-button size="small" @click="handleAdd()" type="primary"><i class="el-icon-plus"></i><span>添加商家</span></el-button>
      </div>
      <el-table 
      v-loading="loading"
      element-loading-text="正在加载"
      element-loading-spinner="el-icon-loading" :data="tableData" :header-cell-style="headClass" :height="tHeight"  style="width:100%"  class="table-box">
        <el-table-column label="id" width="80" prop="id"></el-table-column>
        <el-table-column label="商家名称" prop="name"></el-table-column>
        <el-table-column label="商家类型" width="100" prop="cate"></el-table-column>
        <el-table-column label="门店数" width="80" prop="store_count"></el-table-column>
        <el-table-column label="联系人" prop="link_name"></el-table-column>
        <el-table-column label="联系人电话" width="160" prop="phone"></el-table-column>
        <el-table-column label="人均价格" width="120" prop="price"></el-table-column>
        <el-table-column label="营业时间" width="140">
            <template  slot-scope="scope">
                <p>{{scope.row.start_time}} -- {{scope.row.end_time}}</p>
            </template>
        </el-table-column>
        <el-table-column label="地址" prop="address" width="260"></el-table-column>
        <el-table-column label="收银台配置" width="120">
          <template  slot-scope="scope">
                <div v-if="scope.row.shopAttr">
                  <div v-if="scope.row.shopAttr.pay_config.is_open">
                      <p v-if="scope.row.shopAttr.pay_config.pay.wechat">开启微信支付</p>
                      <p v-else>开启银联支付</p>
                  </div>
                  <div v-else>关闭</div>
                </div>
                <div v-else>关闭</div>
            </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="create_time" width="170"></el-table-column>
        <el-table-column label="操作" width="196">
          <template slot-scope="scope">
            <router-link  :to="{name:'invoiceInformation',query:{id:scope.row.id,type:'shop'}}" @click.native="handleEdit(scope.$index,scope.row)" class="link">发票信息维护</router-link>
            -
            <router-link  :to="{name:'cashierList',query:{id:scope.row.id}}" @click.native="handleEdit(scope.$index,scope.row)" class="link">收银台</router-link>
            -
            <router-link  :to="{name:'businessEdit',query:{id:scope.row.id}}" @click.native="handleEdit(scope.$index,scope.row)" class="link">编辑</router-link>
            -
            <button @click="copyToClip(scope.row.id)" class="butt-min">复制地址</button>
            -
            <button @click="handleRemove(scope.row)" class="butt-min danger">删除</button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pages" ref="page-box">
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10,30,50]"
        :page-size="size"
        layout="total,sizes,prev,pager,next,jumper"
        :total="total"
      ></el-pagination>
      </div>
    </div>
    <router-view @loadData="loadData" :tabShow="tabShow"></router-view>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Provide,Prop,Watch } from "vue-property-decorator";
// import { State, Getter, Mutation, Action } from "vuex-class";
import businessApi from "@/api/business";
@Component({
  components: { }
})
export default class businessList extends Vue {
  // @Getter("user") getUser: any;
  @Provide() state: string = "1"; // 状态默认值
  @Provide() tableData: any = []; // 表格数据
  @Provide() tHeight: number = document.body.offsetHeight - 323;
  @Provide() typeList: any = []; // 商家类型列表
  @Provide() page: number = 1; // 当前page
  @Provide() size: number = 10; // 请求数据的个数 默认5
  @Provide() total: number = 0; // 总数据条数
  @Provide() tabShow: Boolean = true; // 是显示否列表页面
  @Provide() loading: Boolean = true; // 是否显示加载图标
  @Provide() status: number = 0;//禁用状态
  @Prop({}) tabVisible!: any;
  @Provide() searchMap:{
    keyword: string,
    cate_id: string,
    is_child: any,
  }={
    keyword: "",
    cate_id: "",
    is_child: 0
  }
  @Watch("$route") handleRouteChange(to: any,form:any) {
    if(form.path!='/businessList'){
      this.tabShow=this.tabVisible;
    }
  }
  @Provide() searchDatas:{// 查询渠道参数
    market:string,
    channelNo: string,

  }={
    market:"",
    channelNo: "",
  }
  //复制地址
  copyToClip(id) {
    let url = 'pages/storedetails/storedetails?id='+id
    this.$copyText(url)
      .then(()=>{
        this.$message({
          message: '复制成功',
          type: "success"
        });
      })
      .catch(()=>{})
  }
  // 新增
  handleAdd(){
    this.tabShow=false
    this.$router.push({
      path:'/businessList/add'
    })
  }
  headClass() {
            return "text-align: center;background:#eef1f6;";
        }
  // 编辑
  handleEdit(index: number, row: any){
    this.tabShow=false
  }
  // 删除
  handleRemove( data) {
    this.$confirm('此操作将永久删除----'+data.name+'----商家,是否继续？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      closeOnClickModal:false
    }).then(() => {
      businessApi.removeBusiness({id:data.id})
      .then((res: any) => {
        if(res.data.status){
          this.$message({
            message: '操作成功',
            type: "success"
          });
          this.loadData();
        }
      });
    }).catch(() => {
      this.$message({
        type: 'info',
        message: '已取消删除'
      });          
    });
    
  }
  // 禁用启用
  handleDisable(i,row:any){
    if(i==1){
      this.status=1
    }else{
      this.status=0
    }
    businessApi.disabledBusiness(row.id,this.status)
    .then((res: any) => {
      if(res.data.status){
        this.$message({
          message: '操作成功',
          type: "success"
        });
      }
      this.loadData()
    });
  }
  created() {
    this.loadData();
    if(this.$route.path!='/businessList'){
      this.tabShow=false
    }
    businessApi.businessTypeList(1,9999)
      .then((res: any) => {
        this.typeList = res.data.data.list;
      });
  }

  loadData() {
    businessApi.businessList(this.page,this.size,this.searchMap)
      .then((res: any) => {
        this.tableData = res.data.data.list;
        this.total = res.data.data.total;
        this.loading=false
      });
  }

  handleSizeChange(val: number): void {
    this.size = val;
    this.page = 1;
    this.loadData();
  }

  handleCurrentChange(val: number): void {
    this.page = val;
    this.loadData();
  }

  handleSearch(): void {
    // 点击搜索
  this.page = 1;
  this.loadData();
  }
  alltableData() {
    this.searchMap={
      keyword: '',
      cate_id: '',
      is_child: 0,
    }
    this.loadData();
  }
}
</script>

<style lang="scss" scoped>
.el-date-editor{
  margin-right: 10px;

}
.el-button+.el-button {
  margin-top: 10px;
}
.el-table .cell {
    text-align: center !important;
}
</style>
